﻿<Serializable()> Public Class Bauliste
    Inherits ObjektTabelle
    Implements ICloneable
    Public Beschreibung As String()
    Public Werte As Integer()
    Public Sub New(ByVal neuWerte As Integer(), ByVal neuNamen As String())
        Beschreibung = neuNamen
        Werte = neuWerte
    End Sub
    Public Sub New(ByVal Neunamen As String())
        Beschreibung = Neunamen
        Array.Resize(Werte, Beschreibung.Length)
        For I = 0 To Werte.Length - 1
            Werte(I) = 1
        Next
    End Sub
    Public Overrides Function GetWert(ByVal Name As String) As Integer
        For I = 0 To Beschreibung.Length - 1
            If Beschreibung(I) = Name Then
                Return Werte(I)
            End If
        Next
        Return -1
    End Function
    Public Overloads Function GetWert(ByVal Stelle As Integer) As Integer
        If Stelle >= 0 And Stelle < Werte.Length Then
            Return Werte(Stelle)
        End If
        Return -1
    End Function

    Public Overrides Sub SetWert(ByVal Name As String, ByVal Wert As Integer)
        For I = 0 To Beschreibung.Length - 1
            If Beschreibung(I) = Name Then
                Werte(I) = Wert
            End If
        Next
    End Sub
    Public Sub AddWert(ByVal Name As String, ByVal Wert As Integer)
        For I = 0 To Beschreibung.Length - 1
            If Beschreibung(I) = Name Then
                'Wir ändern einfach den Wert.
                Werte(I) = Wert
                Exit Sub
            End If
        Next
        Array.Resize(Beschreibung, Beschreibung.Length + 1)
        Array.Resize(Werte, Werte.Length + 1)
        Beschreibung(Beschreibung.Length - 1) = Name
        Werte(Werte.Length - 1) = Wert
    End Sub
    Public Function Length() As Integer
        Return Beschreibung.Length
    End Function
    Public Overrides Function ToString() As String
        Dim Z As String = ""
        For I = 0 To Beschreibung.Length - 1
            If Werte(I) > 0 Then
                Z += Beschreibung(I) + "_"
            End If
        Next
        If Z.EndsWith("_") Then
            Z = Z.Remove(Z.Length - 1)
        End If
        Return Z
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim S As Bauliste = MemberwiseClone()
        ReDim S.Beschreibung(Beschreibung.Length - 1)
        For I = 0 To Beschreibung.Length - 1
            S.Beschreibung(I) = Beschreibung(I)
        Next
        ReDim S.Werte(Werte.Length - 1)
        For I = 0 To Werte.Length - 1
            S.Werte(I) = Werte(I)
        Next
        Return S
    End Function
End Class
